<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/3/20
 * Time: 10:27
 */

namespace App\Rep;


use App\Models\GoodsShelf;
use App\Models\Warehouse;
use Illuminate\Support\Facades\DB;

class WarehouseRep
{
    /**
     * 获取仓库列表
     * @param $params
     * @return mixed
     */
    public function getWarehouseListRep($params){
        $list = Warehouse::Search($params)->with(['goods_shelf'=>function($q){
            return $q->where('is_del',2);
        }])->orderBy('id','desc')->paginate($params['per_page']??10);
        return $list;
    }

    /**
     * 添加仓库
     * @param $params
     * @return Warehouse|array
     */
    public function createWarehouseRep($params){
        DB::beginTransaction();
        try{
            if(isset($params['id']) && !empty($params['id'])){
                $model = Warehouse::where('id',$params['id'])->first();
                $model->warehouse_name = $params['warehouse_name'];
                $model->save();
            }else{
                $model = new Warehouse();
                $model->warehouse_name = $params['warehouse_name'];
                $model->save();
                GoodsShelf::Create(['warehouse_id'=>$model->id,'goods_shelf_name'=>'货架1']);
            }
            DB::commit();
            return ['code'=>0,'data'=>$model,'msg'=>'success'];
        }catch(\Exception $exception){
            DB::rollBack();
            return ['code'=>2001,'data'=>[],'msg'=>$exception->getMessage()];
        }

    }

    /**
     * 删除仓库
     * @param $params
     */
    public function delWarehouseRep($params){
        DB::beginTransaction();
        try{
            $model = Warehouse::where('id',$params['id'])->first();
            $model->is_del = $params['is_del'];
            $model->save();
            GoodsShelf::where('warehouse_id',$params['id'])->update(['is_del'=>1]);
            //$goods_shelf->save();
            DB::commit();
            return ['code'=>0,'data'=>$model,'msg'=>'success'];
        }catch(\Exception $exception){
            DB::rollBack();
            return ['code'=>2001,'data'=>[],'msg'=>$exception->getMessage()];
        }

    }


    /**
     * 添加货架
     * @param $params
     */
    public function createGoodsShelfRep($params){

        DB::beginTransaction();
        try{
            if(isset($params['goods_shelf_id']) && !empty($params['goods_shelf_id'])){
                $model = GoodsShelf::where('id',$params['goods_shelf_id'])->first();
                $model->warehouse_id = $params['warehouse_id'];
                $model->goods_shelf_name = $params['goods_shelf_name'];
                $model->save();
            }else{
                $model = new GoodsShelf();
                $model->warehouse_id = $params['warehouse_id'];
                $model->goods_shelf_name = $params['goods_shelf_name'];
                $model->save();
            }
            DB::commit();
            return ['code'=>0,'data'=>$model,'msg'=>'success'];
        }catch(\Exception $exception){
            DB::rollBack();
            return ['code'=>2001,'data'=>[],'msg'=>$exception->getMessage()];
        }

    }

    /**删除货架**/
    public function delGoodsShelfRep($params){
        $model = GoodsShelf::where('id',$params['id'])->first();
        $model->is_del = $params['is_del'];
        $model->save();
        return ['code'=>0,'data'=>$model,'msg'=>'success'];
    }
}